package orderProcessingProcess.client.junit;

import static org.junit.Assert.fail;

import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.Endpoint;
import org.apache.cxf.frontend.ClientProxy;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import orderProcessingProcess.exceptions.faults.UnknownCustomerFault;
import orderProcessingProcess.model.Address;
import orderProcessingProcess.model.Customer;
import orderProcessingProcess.model.Payment_Preference;
import orderProcessingProcess.server.NotifyService;

public class TestMailServiceClient {

	private static final QName SERVICE_NAME = new QName(
			"http://infosys.tuwien.ac.at/ait09/ass1/dto/notification",
			"NotificationService");
	private static final QName PORT_NAME = new QName(
			"http://infosys.tuwien.ac.at/ait09/ass1/dto/notification",
			"NotificationPT");

	private static NotifyService mailService;

	// Endpoint Address
	private static String mailServiceAddress = "http://localhost:8089/mailservice";

	@BeforeClass
	public static void init() {
		Service service = Service.create(SERVICE_NAME);

		// Add a port to the Service
		service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING,
				mailServiceAddress);

		mailService = service.getPort(NotifyService.class);

		Client client = ClientProxy.getClient(mailService);
		Endpoint cxfEndpoint = client.getEndpoint();

		cxfEndpoint.getEndpointInfo().setAddress(mailServiceAddress);
	}
	@Test
	public void testMailNotifyCustomer(){
		
		Customer c = this.createCustomer();

		try {
			Assert.assertTrue(mailService.notifyCustomer(c, createTemplateMsg(12)));
		} catch (UnknownCustomerFault e) {
			fail(e.getMessage());
		} catch (orderProcessingProcess.exceptions.faults.MessageTooLongFault e) {
			fail(e.getMessage());
		}
		
	}
	
	@Test
	public void testNotifyServiceUnknownCustomer() {
		Customer c = this.createCustomer();
		c.setId("99999");

		try {
			mailService.notifyCustomer(c, createTemplateMsg(12));
			fail();
		} catch (UnknownCustomerFault e) {
			// Do nothing. The test is ok
		} catch (orderProcessingProcess.exceptions.faults.MessageTooLongFault e) {
			// Do nothing. The test is ok
		}

	}
	
	private Customer createCustomer() {

		Address addr1 = new Address();
		addr1.setId("001");
		addr1.setStreet("1st Avenue");
		addr1.setCity("New Your");
		addr1.setHouse(1);
		addr1.setDoor(1);
		addr1.setZip_code("10000");

		Customer customer1 = new Customer();
		customer1.setId("00001");
		customer1.setName("John Foo");
		customer1.setAddress(addr1);
		customer1.setPaymentPreference(Payment_Preference.Credit_Card);

		// Customer = new Customer(0001, , address, paymentPreference)

		return customer1;

	}

	private String createTemplateMsg(int size) {
		String msg = "";

		for (int i = 0; i < (size / 3); i++) {
			msg += "abc";
		}
		return msg;
	}
}
